package com.enterprise.struts2.admin;

import java.util.Map;

import javax.print.DocFlavor.STRING;

import org.apache.struts2.interceptor.ParameterAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;

import com.enterprise.daoImpl.AdminDaoImple;
import com.enterprise.domain.Admin;
import com.opensymphony.xwork2.ActionSupport;

public class AdminAction extends ActionSupport implements SessionAware,
		ParameterAware, RequestAware {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Map<String, Object> requestMapping = null;
	private Map<String, Object> sessionMapping = null;
	private Map<String, String[]> parameterMapping = null;

	@Override
	public void setRequest(Map<String, Object> arg0) {
		// TODO Auto-generated method stub
		requestMapping = arg0;

	}

	@Override
	public void setSession(Map<String, Object> arg0) {
		// TODO Auto-generated method stub
		sessionMapping = arg0;

	}

	public String save() {

		String name = parameterMapping.get("UserName")[0];
		String password = parameterMapping.get("Password")[0];
		if (name != null && password != null) {
			Admin admin = new Admin();
			admin.setUsername(name);
			admin.setPasswords(password);

			new AdminDaoImple().insertAdmin(admin);
			return "success";
		}
		return "success";

	}

	public String delete() {

		String nameString = parameterMapping.get("UserName")[0];
		System.out.println(nameString);
		String pasString = "";
		Admin admin = new Admin();

		admin.setPasswords(pasString);
		admin.setUsername(nameString);
		System.out.println(admin);
		new AdminDaoImple().delete(admin);
		return "success";
	}

	public String update() {
		AdminDaoImple adminDaoImple = new AdminDaoImple();
		String name = parameterMapping.get("UserName")[0];
		String newPassword = parameterMapping.get("NewPassword")[0];
		String oldPassword = parameterMapping.get("OldPassword")[0];
		if (adminDaoImple.getCounts(new Admin(name, oldPassword))>0) {
			Admin admin = new Admin();
			admin.setUsername(name);
			admin.setPasswords(newPassword);
			if (adminDaoImple.update(admin) > 0) {
				return "success";
			}
			requestMapping.put("updateMessage", "不好意思，系统出错");
			return "success";
		}
		else{
			requestMapping.put("updateMessage", "旧密码不正确！");
			return "success";
		}
	}

	public String execute() {
		return "success";
	}

	@Override
	public void setParameters(Map<String, String[]> arg0) {
		// TODO Auto-generated method stub
		parameterMapping = arg0;
	}

}
